package com.rbac.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbac.admin.common.Result;
import com.rbac.admin.entity.SysRole;
import com.rbac.admin.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色控制器
 */
@RestController
@RequestMapping("/api/roles")
@Tag(name = "角色管理", description = "角色管理")
public class RoleController {

    @Autowired
    private SysRoleService roleService;

    /**
     * 分页查询角色列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param keyword 关键词
     * @return 分页结果
     */
    @Operation(summary = "分页查询角色列表")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<Page<SysRole>> listRolePage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<SysRole> page = roleService.listRolePage(pageNum, pageSize, keyword);
        return Result.success(page);
    }

    /**
     * 获取所有角色列表
     * @return 角色列表
     */
    @Operation(summary = "获取所有角色列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<List<SysRole>> listAllRoles() {
        List<SysRole> roles = roleService.listAllRoles();
        return Result.success(roles);
    }

    /**
     * 获取角色详情
     * @param id 角色ID
     * @return 角色详情
     */
    @Operation(summary = "获取角色详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<SysRole> getRoleById(@PathVariable Long id) {
        SysRole role = roleService.getById(id);
        return Result.success(role);
    }

    /**
     * 添加角色
     * @param role 角色信息
     * @return 添加结果
     */
    @Operation(summary = "添加角色")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:role:add')")
    public Result<Boolean> addRole(@Validated @RequestBody SysRole role) {
        boolean result = roleService.save(role);
        return Result.success(result);
    }

    /**
     * 更新角色
     * @param id 角色ID
     * @param role 角色信息
     * @return 更新结果
     */
    @Operation(summary = "更新角色")
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result<Boolean> updateRole(@PathVariable Long id, @Validated @RequestBody SysRole role) {
        role.setId(id);
        boolean result = roleService.updateById(role);
        return Result.success(result);
    }

    /**
     * 删除角色
     * @param id 角色ID
     * @return 删除结果
     */
    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result<Boolean> deleteRole(@PathVariable Long id) {
        boolean result = roleService.removeById(id);
        return Result.success(result);
    }

    /**
     * 获取角色菜单ID列表
     * @param roleId 角色ID
     * @return 菜单ID列表
     */
    @Operation(summary = "获取角色菜单ID列表")
    @GetMapping("/{roleId}/menus")
    @PreAuthorize("hasAuthority('sys:role:assign')")
    public Result<List<Long>> getMenuIdsByRoleId(@PathVariable Long roleId) {
        List<Long> menuIds = roleService.getMenuIdsByRoleId(roleId);
        return Result.success(menuIds);
    }

    /**
     * 分配角色菜单
     * @param roleId 角色ID
     * @param menuIds 菜单ID列表
     * @return 分配结果
     */
    @Operation(summary = "分配角色菜单")
    @PostMapping("/{roleId}/menus")
    @PreAuthorize("hasAuthority('sys:role:assign')")
    public Result<Boolean> assignMenus(
            @PathVariable Long roleId,
            @RequestBody List<Long> menuIds) {
        boolean result = roleService.assignMenus(roleId, menuIds);
        return Result.success(result);
    }

    /**
     * 获取角色权限ID列表
     * @param roleId 角色ID
     * @return 权限ID列表
     */
    @Operation(summary = "获取角色权限ID列表")
    @GetMapping("/{roleId}/permissions")
    @PreAuthorize("hasAuthority('sys:role:assign')")
    public Result<List<Long>> getPermissionIdsByRoleId(@PathVariable Long roleId) {
        List<Long> permissionIds = roleService.getPermissionIdsByRoleId(roleId);
        return Result.success(permissionIds);
    }

    /**
     * 分配角色权限
     * @param roleId 角色ID
     * @param permissionIds 权限ID列表
     * @return 分配结果
     */
    @Operation(summary = "分配角色权限")
    @PostMapping("/{roleId}/permissions")
    @PreAuthorize("hasAuthority('sys:role:assign')")
    public Result<Boolean> assignPermissions(
            @PathVariable Long roleId,
            @RequestBody List<Long> permissionIds) {
        boolean result = roleService.assignPermissions(roleId, permissionIds);
        return Result.success(result);
    }
}